package com.bacon.gmall.payment.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.bacon.gmall.payment.config.AlipayConfig;
import com.bacon.gmall.payment.service.AlipayService;
import com.bacon.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2021-01-11 21:35
 * @description:
 */
@Controller
@RequestMapping("/api/payment/alipay")
public class AlipayController {

    @Autowired
    private AlipayService alipayService;

    @Autowired
    private PaymentService paymentService;

    //获取订单信息
    @GetMapping("/submit/{orderId}")
    @ResponseBody
    public String submitAlipay(@PathVariable Long orderId) throws AlipayApiException {
        return alipayService.submitAlipay(orderId);
    }

    //同步通知 ，重定向到支付成功页面
    @GetMapping("/callback/return")
    public String callbackReturn() {
        return "redirect:" + AlipayConfig.return_order_url;
    }

    //支付宝异步通知
    @PostMapping("/callback/notify")
    @ResponseBody
    public String receiveNotify(@RequestParam Map<String, String> paramsMap) {

        //编码格式
        String charset = "utf-8";
        //支付宝公钥
        String alipaypublicKey = AlipayConfig.alipay_public_key;
        //签名方式
        String sign_type = "RSA2";
        //调用SDK验证签名 ，若您使用RSA方式签名无需传入sign_type；若您使用RSA2方式签名必须传入sign_type。强烈建议您使用RSA2方式签名。
        boolean signVerified = false;
        try {
            signVerified = AlipaySignature.rsaCheckV1(paramsMap, alipaypublicKey, charset, sign_type);
            if (signVerified) {
                //  验签成功后
                if ("TRADE_SUCCESS".equals(paramsMap.get("trade_status"))){
                    paymentService.updatePaymentInfo(paramsMap);
                }

                System.out.println(paramsMap);
                //{gmt_create=2021-01-12 19:18:53,
                // charset=utf-8,
                // gmt_payment=2021-01-12 19:19:01,
                // notify_time=2021-01-12 19:19:01,
                // subject=Apple iPhone 11 (A2223) 64GB 红色 移动联通电信4G手机 双卡双待 Apple iPhone 11 (A2223) 64GB 黑色 移动联通电信4G手机 双卡双待 ,
                // buyer_id=2088512637134842,
                // invoice_amount=0.01,
                // version=1.0,
                // notify_id=2021011200222191901034841458444989,
                // fund_bill_list=[{"amount":"0.01","fundChannel":"ALIPAYACCOUNT"}],
                // notify_type=trade_status_sync,
                // out_trade_no=userId:3:Bacon:1610450309920:order220,
                // total_amount=0.01, trade_status=TRADE_SUCCESS,
                // trade_no=2021011222001434841450791149,
                // auth_app_id=2021001163617452,
                // receipt_amount=0.01,
                // point_amount=0.00,
                // buyer_pay_amount=0.01,
                // app_id=2021001163617452,
                // seller_id=2088831489324244}
                return "success";
                //按照支付结果异步通知中的描述，对支付结果中的业务内容进行1\2\3\4二次校验，校验成功后在response中返回success，校验失败返回failure
            } else {
                //  验签失败则记录异常日志，并在response中返回failure.
                System.out.println("验签失败");
                return "failure";
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "failure";
    }
}
